/**
 * author:Administrator
 * date:2016/3/2
 * desc:TODO
 */
"use strict";

let Router = require('koa-router')
  , __pathPrefix = '/api/v1/role'
  , router = new Router({prefix: __pathPrefix});
module.exports = router.routes();
console.log('router:' + __pathPrefix + ',file=' + __filename);

let koabody = require('koa-better-body')();
let appUtil = require('../../common/appUtil.js');
let FormatOutput = require('../../common/FormatOutput.js');
let FormatError = require('../../common/FormatError.js');
let platformService = require('../../service/platform');

/**
 * 获取登录用户 的 role
 */
router.get('/', function*() {
  let user = yield appUtil.getUser(this);
  if (!user) return this.body = FormatOutput.payloadOrError(null, FormatError.E_need_login);
  yield _getRolesAndSave(this);

  this.body = new FormatOutput(0, user.roles);
});

function * _getRolesAndSave(ctx) {
  let user = yield appUtil.getUser(ctx);
  if (!user)return;
  let allRoles = user.roles;
  if (allRoles) return;
  let retObj = yield platformService.allUserRole(user.id);
  if (retObj.isOk()) {
    yield appUtil.allRole(ctx, retObj.getPayload());
  }
}

/**
 * 用户选择一个role 做为登录role
 * get /api/v1/role/role/use?type=manager&id=1
 */
router.get('/use', function*() {
  let user = yield appUtil.getUser(this);
  if (!user) return this.body = FormatOutput.payloadOrError(null, FormatError.E_need_login);
  yield _getRolesAndSave(this);

  let type = this.query.type;
  let id = this.query.id;
  let allRoles = user.roles;
  if (!allRoles) {
    return this.body = FormatOutput.payloadOrError(null, FormatError.E_no_bound_role);
  }

  let roles = allRoles && allRoles[type];
  console.log('roles', roles);
  let role = roles.filter(item => item.id == id);
  role = role && role[0];
  console.log('role', role);
  if (role) {
    role.type = type;
    yield appUtil.saveRole(this, role);
    return this.body = new FormatOutput(0, null, role);
  }

  this.body = FormatOutput.payloadOrError(null, FormatError.E_role_not_exist);
});

/**
 * 已登录用户,创建一个 customer
 * name,token
 */
router.post('/customer', koabody, function*() {
  let user = yield appUtil.getUser(this);
  if (!user) return this.body = FormatOutput.payloadOrError(null, FormatError.E_need_login);

  let fields = this.request.body.fields;
  let name = fields.name;

  this.body = yield platformService.createCustomer({name: name, uId: user.id});
});

/**
 * 已登录用户,创建一个 vendor
 * name,token
 */
router.post('/vendor', koabody, function*() {
  let user = yield appUtil.getUser(this);
  if (!user) return this.body = FormatOutput.payloadOrError(null, FormatError.E_need_login);

  let fields = this.request.body.fields;
  let name = fields.name;
  this.body = yield platformService.createVendor({name: name, uId: user.id});
});

/**
 * 已登录用户,创建一个 manager
 * name,token
 */
router.post('/manager', koabody, function*() {
  let user = yield appUtil.getUser(this);
  if (!user) return this.body = FormatOutput.payloadOrError(null, FormatError.E_need_login);

  // 权限检测:必须是已登录manager操作
  console.log(user.role);
  if (!user.role || user.role.type != 'manager') {
    return this.body = FormatOutput.payloadOrError(null, FormatError.E_permission_deny);
  }

  let fields = this.request.body.fields;
  let name = fields.name;
  this.body = yield platformService.createManager({name: name, uId: user.id});

});

/**
 * 已登录用户,创建一个 shopkeeper
 * name,token
 */
router.post('/shopkeeper', koabody, function*() {
  let user = yield appUtil.getUser(this);
  if (!user) return this.body = FormatOutput.payloadOrError(null, FormatError.E_need_login);

  let fields = this.request.body.fields;
  let name = fields.name;
  // 权限检测:
  console.log(user.role);
  if (user.role && user.role.type == 'vendor') {
    // 1 必须是已登录vendor操作,keeper的vendorId 确定
    let vendorId = user.role.id;
    return this.body = yield platformService.createKeeper({name: name, uId: user.id, vendorId: vendorId});
  } else if (user.role && user.role.type == 'manager') {
    // 2 manager 插入数据,keeper无 vendorId
    return this.body = yield platformService.createKeeper({name: name, uId: user.id});
  }
  this.body = FormatOutput.payloadOrError(null, FormatError.E_permission_deny);
});